<template>
	<view class="content">
		<image class="logo" src="/static/logo2.jpeg"></image>
		<view>
			{{playAudioFlag ?'正在播放音频...防止杀后台' :'音频没播放'}}
		</view>
		<h2>手机应用列表</h2>
		<view class="wrap_app">
			<view v-for="item in appLIst" class="item_app">
				<view class="app_name">名称：{{item.appName}}</view>
				<view>包名称：{{item.packageName}}</view>
				<view>versionCode:{{item.versionCode}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	var g_wakelock = null;
	export default {
		data() {
			return {
				title: 'Hello',
				appLIst: [],
				playAudioFlag: false,
				timeRuning: null
			}
		},
		onLoad() {
			this.getAppList()
			this.playAudio()
			this.initRunTime()
		},
		beforeDestroy() {
			this.timeRuning = null //清除定时器
		},
		methods: {
			/**
			 * 定时任务
			 */
			initRunTime() {
				this.timeRuning = setInterval(() => {
					const date = new Date();
					const getHours = date.getHours()
					console.log('getHours', getHours)
					if (20 > getHours >= 19) {
						this.launchApp('钉钉')
					}
					if (9 >= getHours > 8) {
						this.launchApp('钉钉')
					}
				}, 60000)
			},

			//允许程序后台运行 
			wakeLock() {
				//Android  
				var main = plus.android.runtimeMainActivity();
				var Context = plus.android.importClass("android.content.Context");
				var PowerManager = plus.android.importClass("android.os.PowerManager");
				var pm = main.getSystemService(Context.POWER_SERVICE);
				g_wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ANY_NAME");
				g_wakelock.acquire();
			},

			/**
			 * 后台播放音频，防止应用被杀
			 */
			playAudio() {
				plus.device.setVolume(0);
				const bgAudioMannager = uni.getBackgroundAudioManager();
				bgAudioMannager.src = "/static/dida.mp3"; // 配置音频播放路径
				bgAudioMannager.play(); // 播放
				bgAudioMannager.onEnded(function() {
					bgAudioMannager.seek(0);
					bgAudioMannager.play();
				})
				this.playAudioFlag = true
			},

			/**
			 * 获取手机应用列表
			 */
			getAppList() {
				plus.android.importClass('java.util.ArrayList');
				plus.android.importClass('android.content.pm.PackageInfo');
				plus.android.importClass('android.content.pm.PackageManager');
				var ApplicationInfo = plus.android.importClass('android.content.pm.ApplicationInfo');
				var MainActivity = plus.android.runtimeMainActivity();
				var PackageManager = MainActivity.getPackageManager();
				var pinfo = plus.android.invoke(PackageManager, 'getInstalledPackages', 0)
				if (pinfo != null) {
					var apklist = []
					for (var i = 0; i < pinfo.size(); i++) {
						//PackageInfo{4b45699f9d  com.tencent.mobileqq}    
						var pkginfo = pinfo.get(i);
						var issysapk = ((pkginfo.plusGetAttribute("applicationInfo").plusGetAttribute("flags") &
							ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false
						if (issysapk == false) {
							const apkinfo = {
								appName: pkginfo.plusGetAttribute("applicationInfo").loadLabel(PackageManager)
									.toString(),
								packageName: pkginfo.plusGetAttribute("packageName"),
								versionName: pkginfo.plusGetAttribute("versionName"),
								versionCode: pkginfo.plusGetAttribute("versionCode")
							}
							apklist.push(apkinfo)
						}

					}
					//this.appList = apklist  
					//打印出所有的APP名称，包名，版本  
					console.log('apkinfo', apklist)
					this.appLIst = apklist
				}
			},
			/**
			 * 根据应用名称找到应用包名称，打开对应的手机应用，默认为钉钉
			 * @param {Object} name
			 */
			launchApp(name) {
				let packageName = '钉钉'
				this.appLIst.map(item => {
					if (item.appName === name) {
						packageName = item.packageName
					}
				})
				if (plus.os.name == 'Android') {
					plus.runtime.launchApplication({
							pname: packageName
						},
						function(e) {
							console.log('Open system default browser failed: ' + e.message);
						}
					);
				} else if (plus.os.name == 'iOS') {
					plus.runtime.launchApplication({
						action: packageName
					}, function(e) {
						console.log('Open system default browser failed: ' + e.message);
					});
				}
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin: 50rpx auto 50rpx auto;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.wrap_app {
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		align-items: center;
	}

	.item_app {
		width: 100%;
		padding: 8px 2px;
	}

	.app_name {
		color: #007AFF;
		font-size: 24px;
	}
</style>
